package com.carsys.controller;

import com.carsys.dto.ActionResult;
import com.carsys.entity.Car;
import com.carsys.service.CarService;
import com.carsys.vo.VCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CarController {
    @Autowired
    private CarService carService;

    /**
     * 根据价格查询车辆
     * @param getid
     * @return ActionResult
     *
     */
    @RequestMapping(value = "/getprice",method = RequestMethod.GET)
    public ActionResult findCarByPrice(int getid){
        List<Car> cities = carService.findCarPrice(getid);

        ActionResult result = new ActionResult(200, null, cities);
        return result;
    }

    /**
     * 根据数量查询车辆
     * @param getid
     * @return ActionResult
     */
    @RequestMapping(value = "/getnumber",method = RequestMethod.GET)
    public ActionResult findCarByNumber(int getid){
        List<Car> cities = carService.findCarNumber(getid);

        ActionResult result = new ActionResult(200, null, cities);
        return result;
    }

    @RequestMapping("/findCar")
    @ResponseBody
    public ActionResult findCar(int cid) {

            Car car = carService.findCarById(cid);

            if(car!=null){
                return new ActionResult(200,"获取到了",car);
            }
            return null;
    }

    /**
     * 根据类型查询汽车
     * @return 汽车对象
     */
    @RequestMapping(value = "/getCars",method = RequestMethod.GET)
    public List<VCar> findCars(){
      List<VCar> car = carService.findCars();
      return car;
    }

    /**
     * 根据类型删除车辆
     * @param id
     * @return
     */
    @RequestMapping(value = "/delCars",method = RequestMethod.DELETE)
    public ActionResult delteOrders(int id){
        boolean bl = carService.delteOrders(id);
       return new ActionResult(200,"删除成功",null);
    }

    /**
     * 根据id查询车辆
     * @param id
     * @return
     */
    @RequestMapping(value = "/getCarInfoById/{id}",method = RequestMethod.GET)
    public ActionResult findCarInfoById (@PathVariable("id") int id){
      VCar cars=carService.findCarInfoById(id);

        return new ActionResult(200,null,cars);

    }

    /**
     * 修改车辆信息
     * @param cars
     * @return
     */
    @RequestMapping(value="/modify",method=RequestMethod.PUT)
    public ActionResult modify(@RequestBody VCar cars){

        carService.updataCar(cars);
        return new ActionResult(200,"更新成功！",null);
    }
    /**
     * 添加车辆信息
     * @param car
     * @return
     */
     @RequestMapping(value="/addCar",method = RequestMethod.POST)
    public ActionResult addCar(Car car){

        if(carService.addCar(car)){

            return new ActionResult(200,"添加成功",null);
        }

        return null;
    }
}
